/**
 * Created by hy1 on 2018/4/12.
 */
$().ready(function(){
	$("[data-href]").on("click", function (e) {
        e.stopPropagation();location.href=$(this).data("href");
    });
	$.ajaxSetup({
		error:function(r){
			if(r.status==403){
				var token=sessionStorage.getItem("token");
				sessionStorage.clear();
				var url=r.getResponseHeader("CONTENTPATH");
				token?url=url+"?login=true":url=url+"?login=false";
				location.href=url;
			}
		}
	})
	
});
$().ready(function(){
		function dealAddress(str){
			if(str.length<12){return str;}
			//至少12位
			var start=str.substr(0,6)+"……"+str.substr(str.length-6);
			return start;
		}
		function getPage(num){
			$.ajax({
				type : "POST",  //提交方式    limit
				url:"/app/order/allorder",
				data:{"limit":num},
	 			success:function(r){
	 				//console.log(r)
	 				$(".loading-box").css("display","none");
	 				if(r.code==0){
	 					var data=r.list;
	 	 				var html="";
	 	 				for(var i=0,len=data.length;i<len;i++){
	 	 					var status=data[i].orderstatus;
	 	 					var statusHtml="";
	 	 					switch(status){
	 	 					case(1):statusHtml="<h4 class='gold'> 转换中 </h4>";break;
	 	 					case(2):statusHtml="<h4 class='gold'> 处理中 </h4>";break;
	 	 					case(3):statusHtml="<h4>已完成 </h4>";break;
	 	 					case(4):statusHtml="<h4>订单失败</h4>";break;
	 	 					}
	 	 					var targetaddHtml=dealAddress(data[i].targetadd);
	 	 					var refundaddHtml=dealAddress(data[i].refundadd);
	 	 					html+=`<li class="fix-clear">
	 	 		            <div class="pull-left">
	 	 		               <h4> ${data[i].from} <img src="../../img/right-translate2.png" style="margin-right:5px;" alt=""/>${data[i].to}  </h4>
	 	 		                <p>目标地址 ${targetaddHtml}</p>
	 	 		                <p>退款地址 ${refundaddHtml}</p>
	 	 		            </div>
	 	 		            <div class="pull-right">
	 	 		               ${statusHtml}
	 	 		                <p>${data[i].ordertime}</p>
	 	 		            </div>
	 	 		        </li>`;
	 	 				}
	 	 				$(".token-record-ul").html(html);
	 	 				$(".token-record-ul").unbind("click").on("click","li",function(){
	 	 					var icode=$(this).index(); var code=data[icode].code;
	 	 					//console.log("code",icode,code);
	 	 					location.href="Token_Trading_History_Order.html?order="+code
	 	 				})
	 				}else{
	 					alert(r.msg);
	 				}
	 			}
	 		})
		}
		var pageNum=5;
		getPage(pageNum);
 		//下滑加载更多
 		var pagex={};
 		window.addEventListener("touchstart",function(e){
			e.stopPropagation();
			 var disX=e.changedTouches[0].pageX;
		     var disY=e.changedTouches[0].pageY;
		     pagex.x=disX;
		     pagex.y=disY;
		})
		window.addEventListener("touchend",function(e){
			e.stopPropagation();
			 var disX=e.changedTouches[0].pageX;
		     var disY=e.changedTouches[0].pageY;
		     var plus=disY-(pagex.y);    
		     pageNum+=5;
		     if(plus<-60){
		    	 getPage(pageNum);
		     }	
		})
 		
	});